package com.klikmee.sms.chat.web.usecases;

import java.util.ArrayList;

import org.json.JSONException;

import com.dotexe.webframework.core.ClientApplication;
import com.dotexe.webframework.core.datasources.ObjectDataSource;
import com.dotexe.webframework.core.triggers.Trigger;
import com.klikmee.sms.chat.web.dao.GroupDAO;
import com.klikmee.sms.chat.web.util.SpringBeansLookup;
import com.klikmee.sms.services.chat.model.Group;

public class GroupDetailsForm extends ClientApplication {

	private ObjectDataSource dsGroup;
	private ArrayList<String> bcGroup;

	private Group group;
	private boolean editGroup;
	
	// override
	@Override
	public void load() throws Exception {
		super.load();
		
		this.dsGroup = new ObjectDataSource("dsGroup");
		this.getDataSources().add(this.dsGroup);

		this.bcGroup = new ArrayList<String>();
		this.bcGroup.add("GroupName");
		this.bcGroup.add("GroupDescription");
		this.bcGroup.add("Active");
		this.bcGroup.add("Administration");
		
		this.triggers.add(new Trigger(this, "btnSaveClick"));
		this.triggers.add(new Trigger(this, "btnCancelClick"));
	}

	@Override
	public void start() throws Exception {
		super.start();
		
		this.dsGroup.setSource(group);
		this.dsGroup.bind(bcGroup);
	}
	
	public void btnSaveClick() throws JSONException
	{
		GroupDAO groupDAO = SpringBeansLookup.getBean(GroupDAO.class);
		
		group.setGroupName(((Group) this.dsGroup.getSource()).getGroupName());
		group.setGroupDescription(((Group) this.dsGroup.getSource()).getGroupDescription());
		group.setActive(((Group) this.dsGroup.getSource()).getActive());
		
		if (editGroup) {
			groupDAO.edit(group);
		} else {
			groupDAO.create(group);
		}

		this.hide();
		
	}
	public void btnCancelClick() throws JSONException
	{
		this.hide();
	}

	
	public void setGroup(Group group) {
		this.group = group;
	}

	public Group getGroup() {
		return group;
	}

	
	public void setEditGroup(boolean editGroup) {
		this.editGroup = editGroup;
	}

	public boolean getEditGroup() {
		return editGroup;
	}
}
